//
//  WKNomalTableViewCell.m
//  WeKeTang
//
//  Created by ichina on 2017/9/19.
//  Copyright © 2017年 Dezhi Tiancheng Education Technology (Beijing) Co. Ltd. All rights reserved.
//

#import "WKNomalTableViewCell.h"
#import "WKNetworkTools.h"
#import <UIImageView+WebCache.h>

@interface WKNomalTableViewCell ()

@property (nonatomic, strong) UIImageView *iconImageView;

@property (nonatomic, strong) YYLabel *titleLabel;

@property (nonatomic, strong) UIImageView *timeImgView;

@property (nonatomic, strong) YYLabel *timeLabel;

@property (nonatomic, strong) UIButton *playerBtn;

@end

@implementation WKNomalTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self setupUI];

    }
    return self;
}

- (void)setupUI{
    
    self.iconImageView = [[UIImageView alloc]init];
    [self.contentView addSubview:self.iconImageView];
    
    self.titleLabel = [[YYLabel alloc]init];
    [self.contentView addSubview:self.titleLabel];
    self.titleLabel.font = [UIFont systemFontOfSize:13];
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.textColor = [UIColor blackColor];
    
    self.timeImgView = [[UIImageView alloc]init];
    self.timeImgView.image = [UIImage imageNamed:@"icon_time"];
    [self.contentView addSubview:self.timeImgView];
    
    self.timeLabel = [[YYLabel alloc]init];
    self.timeLabel.font = [UIFont systemFontOfSize:13];
    self.timeLabel.textColor = [UIColor colorWithHexString:@"#FDA60A"];
//    self.timeLabel.text = @"剩余04:16未观看";
    [self.contentView addSubview:self.timeLabel];
    
    self.playerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.playerBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    
    [self.contentView addSubview:self.playerBtn];
  
}

- (void)setModel:(WKDownLoadModel *)model{
    
    _model = model;
    
    [self.iconImageView setImageWithURL:[NSURL URLWithString:model.section_imgurl] options:YYWebImageOptionShowNetworkActivity];
    if ([model.currentWatchCount isEqualToString:model.totalWatchCount]) {
        self.timeImgView.image = [UIImage imageNamed:@"icon_timeover"];
        self.timeLabel.text = @"已看完";
        self.timeLabel.textColor = RGBColor(156, 156, 156);
        
    }else if ([model.currentWatchCount isEqualToString:@"0"]){
        
        self.timeImgView.image = [UIImage imageNamed:@"icon_timeunread"];
        self.timeLabel.text = @"未观看";
        self.timeLabel.textColor = RGBColor(1, 186, 202);
        
    }else{
        self.timeImgView.image = [UIImage imageNamed:@"icon_time"];
        NSInteger time = [model.totalWatchCount integerValue] - [model.currentWatchCount integerValue];
        
        NSString *timeStr = [NSString stringWithFormat:@"%ld",time];
        
        self.timeLabel.text =  [self getMMSSFromSS:timeStr];
        NSLog(@"剩余多长时间：%@",timeStr);
        
        
    }
    self.titleLabel.text = model.section_name;
}
-(NSString *)getMMSSFromSS:(NSString *)totalTime{
    
    NSInteger seconds = [totalTime integerValue];
    
    NSString *str_minute = [NSString stringWithFormat:@"%ld",seconds/60];
    
    NSString *str_second = [NSString stringWithFormat:@"%ld",seconds%60];
    
    NSString *format_time = [NSString stringWithFormat:@"剩余%@:%@未观看",str_minute,str_second];
    
    NSLog(@"format_time : %@",format_time);
    
    return format_time;
}
- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    self.iconImageView.frame =CGRectMake(10, 15, 140, 120-30);
    self.titleLabel.frame =CGRectMake(CGRectGetMaxX(self.iconImageView.frame)+10, 5, kDeviceScreenWidth-20-140-10, 42);
    self.timeImgView.frame = CGRectMake(CGRectGetMaxX(self.iconImageView.frame)+10, CGRectGetMaxY(self.iconImageView.frame)-10, 11, 11);
    self.timeLabel.frame = CGRectMake(CGRectGetMaxX(self.timeImgView.frame)+10, CGRectGetMaxY(self.iconImageView.frame)-15, kDeviceScreenWidth-20-140-10-11-10, 21);
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    
    
}

@end
